import pygame
import ui_backend
import graphicalscene
import input
import gamestate as gs_module
import soundmachine

STATE_INTRO = 0
STATE_TITLE = 1
STATE_GRAPHICAL_SELECT_ACTION = 2
STATE_GRAPHICAL_ANIMATE_ACTION = 3
STATE_TURN_SUMMARY = 4
STATE_GAME_OVER = 5




class StateBase(object):
    def entry(self):
        pass
    
    def do(self):
        pass
    
    def exit(self):
        pass


class SimpleTextState(StateBase):
    def __init__(self, text, pos, nextStateId):
        self.text = text
        self.pos = pos
        self.nextStateId = nextStateId
        
    def do (self):
        ui_backend.screen.fill(ui_backend.BLACK)
        ui_backend.draw_text(self.text, *self.pos)
        ui_backend.draw_tooltip("Click when ready", input.mousePos)
        
        if input.didClick:
            return self.nextStateId
        else:
            return None


class TitleState(SimpleTextState):
    def entry(self):
        soundmachine.SoundMachine.PlayInitialSound()
    
    def exit(self):
        graphicalscene.initialize_for_new_game()

    
class SelectActionGraphicalState(StateBase):
    def entry(self):
        gamestate = graphicalscene.gamestate
        reactors = graphicalscene.reactors
        for guiReactor, gamestateReactor in zip(reactors, gamestate.GetReactors()):
            guiReactor.set_state(gamestateReactor.endstate)
                    
    
    def do(self):
        gamestate = graphicalscene.gamestate
        reactors = graphicalscene.reactors
        
        ui_backend.screen.blit(graphicalscene.bg, (0,0))
        for reactor in reactors: reactor.draw(ui_backend.screen)
        
        tooltiptext = "..."
        
        didClickAction = False
        
        for elem in gamestate.actions:
            isHit = elem.rect.collidepoint(input.mousePos)
            isPossible = elem.is_possible()
            if isHit:
                tooltiptext = elem.get_label()
                if input.didClick and isPossible: 
                    gamestate.perform_action(elem)
                    gamestate.tick() #perform one round
                    didClickAction = True
            if elem.is_visible():
                graphicalscene.draw_action_box(elem, isHit and isPossible)
        
        # animate worker:
        graphicalscene.worker.move_to((input.mousePos[0] -20, input.mousePos[1] -20))
        if gs_module.GlobalStates.Radiation_Level > 999:
            graphicalscene.worker.die()
        graphicalscene.worker.tick(ui_backend.screen)
        
        graphicalscene.helicopter.tick(ui_backend.screen)
        
        ui_backend.draw_tooltip(tooltiptext, input.mousePos)
        
        
        # draw HUD:
        fpsText = "%i FPS" % ui_backend.clock.get_fps()
        ui_backend.draw_text(fpsText, 10, 10)
            
        ui_backend.draw_text("Radiation (milli Sievert):  %i" % gs_module.GlobalStates.Radiation_Level, 10, 40)
        ui_backend.draw_text("Core Temperature (degree Celsius) %i" % gs_module.GlobalStates.Core_Temperature, 10, 70)
        
        if didClickAction:
            return STATE_GRAPHICAL_ANIMATE_ACTION
        else:
            return None

    
    def exit(self):
        pass


class AnimateActionGraphicalState(StateBase):
    def entry(self):
        pass
    
    def do(self):
        return STATE_TURN_SUMMARY
    
    def exit(self):
        pass
    
class TurnSummaryState(StateBase):
    def do(self):
        
        #graphical_scene.game_state.Assemble
		#gs_module.GlobalStates.
	    
		# TODO: MM add code here
        # ----------
        ui_backend.screen.fill(ui_backend.BLACK)
        ui_backend.draw_text_big("Turn summary", 312, 10)
        top_y = 50;
        for line in gs_module.GlobalStates.AssembleStatusReport():		
            if line.startswith('\t'):
              top_y = top_y + 15*ui_backend.draw_text_max_width(line.replace('\t',''),200,top_y,460)
              top_y = top_y + 5
            else:
               top_y = top_y + 15 * ui_backend.draw_text_max_width(line,150,top_y,510)
            if top_y == 80 :
			   top_y = top_y + 20
        
        ui_backend.draw_tooltip("Click when ready", input.mousePos)
        # ----------
        
        if input.didClick:
            if graphicalscene.gamestate.is_game_over():
                return STATE_GAME_OVER
            else:
                return STATE_GRAPHICAL_SELECT_ACTION
        else:
            return None
    
class GameOverState(StateBase):
    def do(self):
        
        # TODO: MM add code here
        # ----------
        ui_backend.screen.fill(ui_backend.BLACK)
        ui_backend.draw_text_big("Game Over", 331, 160)
        ui_backend.draw_text("Radiation (milli Sievert): %i" % gs_module.GlobalStates.Radiation_Level, 250, 230)
        ui_backend.draw_text("Core Temperature (degree Celsius): %i" % gs_module.GlobalStates.Core_Temperature, 250, 245)
        ui_backend.draw_text('Your actions were:',343,290)
        
        y = 0
        nr = 1
        for act in gs_module.GlobalStates.CounterMeasuresActionList:
            ui_backend.draw_text(str(nr) + '. ' + act.get_label()[:act.get_label().rfind('(')],250,310+y)
            y = y + 15
            nr = nr + 1
		
        ui_backend.draw_tooltip("Click when ready", input.mousePos)
        # ----------
        
        if input.didClick :
            return STATE_INTRO
        else:
            return None

